<?php


class b2c_ctl_wap_amember extends b2c_ctl_wap_common{  

    function __construct(&$app){
        parent::__construct($app);
      
        $shopname = app::get('wap')->getConf('wap.name');
        if(isset($shopname)){
            $this->title = app::get('b2c')->_('会员中心').'_'.$shopname;
            $this->keywords = app::get('b2c')->_('会员中心_').'_'.$shopname;
            $this->description = app::get('b2c')->_('会员中心_').'_'.$shopname;
        }
        $this->verify_member();
        $this->pagesize = 10;
        $this->action = $this->_request->get_act_name();
        if(!$this->action) $this->action = 'index';
        $this->action_view = $this->action.".html";
        $this->member = $this->get_current_member();
        $this->pagedata['res_url'] = $this->app->res_url;
        /** end **/
    }
    public function index(){
        $input['method'] = array("file"=>"mobileapi_rpc_goods","action"=>"get_member_info");
        $input['data'] = array('member_id'=>$this->member['member_id']);
        $member = $this->getApi($input);
        $input['method'] = array("file"=>"mobileapi_rpc_member","action"=>"favorite");
        $input['data'] = array('mode'=>'yts');
        $fav = $this->getApi($input);
        $fav = $fav['data']['products']['items'];
        $input['method'] = array("file"=>"mobileapi_rpc_goods","action"=>"getopinionsformember");
        $input['data'] = array('member_id'=>$this->member['member_id']);
        $opinions = $this->getApi($input);
        $this->pagedata['fav'] = $fav;
        $this->pagedata['opinions'] = $opinions['data'];
        $this->pagedata['member'] = $member['data'];
        $this->page('wap/amember/myfav.html');
        
    }
    function cart(){
        $this->begin(array('app'=>'b2c','ctl'=>'wap_agoods','act'=>'cart'));
        $sku = trim($_POST['sku'],';');
        $skunum = $_POST['skunum'];
        $product_id = 0;
        if($_POST['sku'] && $_POST['goods_id']){
            $input['method'] = array("file"=>"mobileapi_rpc_goods","action"=>"get_item");
            $input['data'] = array("iid"=>$_POST['goods_id'],'son_object'=>'json');
            $goods = $this->getApi($input);
            $sku_a = json_decode($goods['data']['item']['skus'],true);
            foreach($sku_a as $_sku){
                if($sku == $_sku['properties']){
                    $product_id = $_sku['sku_id'];
                }
            }
            if($product_id){
                $input['method'] = array("file"=>"mobileapi_rpc_cart","action"=>"add");
                $input['data'] = array("product_id"=>$product_id,'num'=>$skunum);
                $add = $this->getApi($input);
                if(!$add['status']){
                    $this->end(false, app::get('b2c')->_('参数错误'));
                }
            }else{
                $this->end(false, app::get('b2c')->_('规格不存在'));
            }
        }elseif($_POST['goods_id']){
            
            $input['method'] = array("file"=>"mobileapi_rpc_goods","action"=>"get_item");
            $input['data'] = array("iid"=>$_POST['goods_id'],'son_object'=>'json');
            $goods = $this->getApi($input);
            
            $sku_a = json_decode($goods['data']['item']['skus'],true);
            $product_id = $sku_a[0]['sku_id'];
            if($product_id){
                $input['method'] = array("file"=>"mobileapi_rpc_cart","action"=>"add");
                $input['data'] = array("product_id"=>$product_id,'num'=>$skunum);
                $add = $this->getApi($input);
                if(!$add['status']){
                    $this->end(false,app::get('b2c')->_('参数错误'));
                }
            }else{
                
                $this->end(false,app::get('b2c')->_('规格不存在'));
            }
        }
        $input['method'] = array("file"=>"mobileapi_rpc_cart","action"=>"get_list_group_by_tip");
        $input['data'] = array();
        $cart = $this->getApi($input);
        $this->pagedata['cart'] = $cart['data'];
        $this->page('wap/amember/cart.html');
        
        
    }
    function checkout(){
        if($_POST['obj_ident']){
            foreach($_POST['obj_ident'] as $val){
                $out = $val;
            }
            $input['method'] = array("file"=>"mobileapi_rpc_cart","action"=>"get_list");
            $input['data'] = array();
            $cart = $this->getApi($input);
            $arr = array();
            $ex = explode(",", trim($_POST['tip'],','));
            foreach($cart['data']['object']['goods'] as $goods){
            if(!in_array($goods['obj_ident'],$out) || !in_array($goods['obj_ident'],$ex)){
                    $arr[] = array(
                        'obj_type' => 'goods',
                        'obj_ident' => $goods['obj_ident']
                    );
                }
            }
            if($arr){
                $input['method'] = array("file"=>"mobileapi_rpc_cart","action"=>"batch_remove");
                $input['data'] = array('items'=>  json_encode($arr));
                $re = $this->getApi($input);
            }
        }
        
        $input['method'] = array("file"=>"mobileapi_rpc_cart","action"=>"checkout");
        $input['data'] = array();
        $checkout = $this->getApi($input);
        
        $this->pagedata['checkout'] = $checkout['data'];
        $this->pagedata['cookie'] = json_decode($_COOKIE['cart']);
        $this->page('wap/amember/checkout.html');
        
    }
    public function checkgoods(){
        $input['method'] = array("file"=>"mobileapi_rpc_goods","action"=>"get_goods_for_order");
        $input['data'] = array('type'=>'all');
        $goodslist = $this->getApi($input);
        $this->pagedata['goodslist'] = $goodslist;
        $this->page('wap/amember/checkgoods.html');
    }
    function addopinions(){
        return "还没做";
        $this->page('wap/amember/addopinions.html');
    }
    function myfav($member_id=0){
        $input['method'] = array("file"=>"mobileapi_rpc_goods","action"=>"get_member_info");
        $input['data'] = $member_id ? array('member_id'=>$member_id) : array('member_id'=>$this->member['member_id']);
        $member = $this->getApi($input);
        $input['method'] = array("file"=>"mobileapi_rpc_member","action"=>"favorite");
        $input['data'] = array('mode'=>'yts');
        $fav = $this->getApi($input);
        $fav = $fav['data']['products']['items'];
        $input['method'] = array("file"=>"mobileapi_rpc_goods","action"=>"getopinionsformember");
        $input['data'] = array('member_id'=>$this->member['member_id']);
        $opinions = $this->getApi($input);
        $this->pagedata['fav'] = $fav;
        $this->pagedata['opinions'] = $opinions['data'];
        $this->pagedata['member'] = $member['data'];
        $this->page('wap/amember/myfav.html');
    }
    function addr(){
        $input['method'] = array("file"=>"mobileapi_rpc_member","action"=>"receiver");
        $input['data'] = array();
        $addrs = $this->getApi($input);
        $this->pagedata['addrs'] = $addrs['data'];
        $this->page('wap/amember/addr.html');
    }
    function checkaddr($addr_id){
        if($addr_id){
           $is_addr = app::get("b2c")->model("member_addrs")->count(array("addr_id"=>$addr_id,'member_id'=>$this->member['member_id']));
           if(!$is_addr){
               $this->splash('error',null, app::get('b2c')->_('地址id错误'),true);
           }
        }else{
            $this->splash('error',null, app::get('b2c')->_('参数错误'),true);
        }
        $input['method'] = array("file"=>"mobileapi_rpc_member","action"=>"modify_receiver");
        $input['data'] = array('addr_id'=>$addr_id);
        $addrs = $this->getApi($input);
        if($addrs['status']){
            $area = explode(":", $addrs['data']['area']);
            $addr['area'] = $area[2];
            $addr['addr_id'] = $addr_id;
            //TODO 记录选择地址
            $ck = json_decode($_COOKIE['cart'],true);
            $addr = app::get("b2c")->model('member_addrs')->getRow("*",array('addr_id'=>$addr_id));
            $ck['addr'] = $addr;
            setcookie("cart",  json_encode($ck));
            echo json_encode($addrs);
        }
    }
    function addradd(){
        $sql="select region_id,region_grade,local_name,ordernum,(select count(*) from sdb_ectools_regions where p_region_id=r.region_id) as child_count from sdb_ectools_regions as r "
                . "where r.p_region_id is null order by ordernum asc,region_id";
            
        $row = kernel::database()->select($sql);
        $this->pagedata['regions']['p'] = $row;
        $this->page('wap/amember/addredit.html');
        
    }
    function addredit($addr_id){
        $this->begin(array('app'=>'b2c','ctl'=>'wap_amember','act'=>'addr'));
        if($addr_id){
           $is_addr = app::get("b2c")->model("member_addrs")->count(array("addr_id"=>$addr_id,'member_id'=>$this->member['member_id']));
           if(!$is_addr){
               $this->end(false,app::get('b2c')->_("地址id错误"),$this->gen_url(array('app'=>'b2c','ctl'=>'wap_amember','act'=>'addr')),true,true);
           }
        }else{
            $this->end(false,app::get('b2c')->_("参数错误"),$this->gen_url(array('app'=>'b2c','ctl'=>'wap_amember','act'=>'addr')),true,true);
        }
        $input['method'] = array("file"=>"mobileapi_rpc_member","action"=>"modify_receiver");
        $input['data'] = array('addr_id'=>$addr_id);
        $addrs = $this->getApi($input);
        $this->pagedata['addrs'] = $addrs['data'];
        $region_id = explode(":", $addrs['data']['area']);
        $region = app::get("ectools")->model("regions")->getRow("*",array("region_id"=>$region_id[2]));
        $sql="select region_id,region_grade,local_name,ordernum,(select count(*) from sdb_ectools_regions where p_region_id=r.region_id) as child_count from sdb_ectools_regions as r "
                . "where r.p_region_id is null order by ordernum asc,region_id";
            
        $row1 = kernel::database()->select($sql);
        $this->pagedata['regions']['p'] = $row1;
        $re = explode(",", $region['region_path']);
        if($re[1] && $re[1] !== $region['region_id']){
            $sql="select region_id,region_grade,local_name,region_path,ordernum,(select count(*) from sdb_ectools_regions where p_region_id=r.region_id) as child_count from sdb_ectools_regions as r "
                    . "where r.p_region_id = '".$re[1]."' order by ordernum asc,region_id";

            $row2 = kernel::database()->select($sql);
            $this->pagedata['regions']['c'] = $row2;
        }
        if($re[2] && $re[2] !== $region['region_id']){
            $sql="select region_id,region_grade,local_name,region_path,ordernum,(select count(*) from sdb_ectools_regions where p_region_id=r.region_id) as child_count from sdb_ectools_regions as r "
                    . "where r.p_region_id = '".$re[2]."' order by ordernum asc,region_id";

            $row3 = kernel::database()->select($sql);
            $this->pagedata['regions']['d'] = $row3;
        }
        $this->pagedata['city'] = $re;
        $this->page('wap/amember/addredit.html');
        
    }
    function addrupdate(){
        $this->begin(array('app'=>'b2c','ctl'=>'wap_amember','act'=>'addr'));
        $regions = app::get("ectools")->model("regions");
        $inputs = array(
            'addr_id' => (int)$_POST['addr_id'],
            'name' => trim($_POST['name']),
            'mobile' => $_POST['phone'],
            'area' => !$_POST['district'] ? "mainland:".$this->getregionsname($_POST['province'])."/".$this->getregionsname($_POST['city']).":".$_POST['city'] : "mainland:".$this->getregionsname($_POST['province'])."/".$this->getregionsname($_POST['city'])."/".$this->getregionsname($_POST['district']).":".$_POST['district'],
            'addr' => $_POST['deta_address'],
            'card_num' => trim($_POST['ID_card']),
            
        );
        $region = $regions->count(array('member_id'=>$this->member['member_id']));
        if(!$region){
            $inputs['def_addr'] = 1;
        }else{
            $inputs['def_addr'] = 0;
        }
        
        $save_data = kernel::single('b2c_member_addrs')->purchase_save_addr($inputs,$this->member['member_id'],$msg);
        if(!$save_data){
            $this->end(false, app::get('b2c')->_('地址保存失败'));
        }else{
            $this->end(true);
        }
        
        
    }
    function getregions($p_regionid){
        
    	if ($p_regionid)
            $sql="select region_id,region_grade,local_name,ordernum,(select count(*) from sdb_ectools_regions where p_region_id=r.region_id) as child_count from sdb_ectools_regions as r where r.p_region_id=".intval($p_regionid)." order by ordernum asc,region_id";
        else
            $sql="select region_id,region_grade,local_name,ordernum,(select count(*) from sdb_ectools_regions where p_region_id=r.region_id) as child_count from sdb_ectools_regions as r where r.p_region_id is null order by ordernum asc,region_id";
            
        $row = kernel::database()->select($sql);
        echo json_encode($row);
        
    }
    function getregionsname($regionid){
        $region = app::get("ectools")->model("regions")->getRow("*",array('region_id'=>$regionid));
        return $region['local_name'];
    }
    function coupon(){
        //兑换优惠券
        if($_REQUEST['coupon']){
            
            $input['method'] = array("file"=>"mobileapi_rpc_member","action"=>"coupon_code");
            $input['data'] = array("coupon"=>$_POST['coupon']);
            $re = $this->getApi($input);
            if(!$re['status']){
                $this->pagedata['error'] = "优惠码无效";
            }else{
                
                $this->pagedata['success'] = "兑换成功";
            }
        }
        $input['method'] = array("file"=>"mobileapi_rpc_member","action"=>"coupon");
        $input['data'] = array('memc_used_times'=>0);
        $coupon = $this->getApi($input);
        $this->pagedata['coupon'] = $coupon['data'];
        $this->pagedata['thiscoupon'] = $_GET['thiscoupon'];
        
        $this->page('wap/amember/coupon.html');
        
    }
    function check_coupon(){
        //兑换优惠券
        $input['method'] = array("file"=>"mobileapi_rpc_cart","action"=>"checkout");
        $input['data'] = array();
        $checkout = $this->getApi($input);
        $old = $checkout['data']['aCart']['object']['coupon'][0]['coupon'];
        //选择优惠券
        $input['method'] = array("file"=>"mobileapi_rpc_cart","action"=>"add_coupon");
        $input['data'] = array('coupon'=>$_POST['coupon_id']);
        if($old){
            $input['data']['old_coupon'] = $old;
        }
        $coupon = $this->getApi($input);

        echo "<script>document.location='".kernel::base_url(1).$this->gen_url(array('app'=>'b2c','ctl'=>'wap_amember','act'=>'checkout'))."'</script>";

    }
    function usecoupon(){
        //可使用的优惠券
        $input['method'] = array("file"=>"mobileapi_rpc_member","action"=>"coupon");
        $input['data'] = array('memc_used_times'=>0);
        $coupon = $this->getApi($input);
        $this->pagedata['coupon'] = $coupon['data'];
        
        $this->page('wap/amember/usecoupon.html');
        
    }
    function historycoupon(){
        //可使用的优惠券
        $input['method'] = array("file"=>"mobileapi_rpc_member","action"=>"coupon");
        $input['data'] = array('memc_used_times'=>1);
        $coupon = $this->getApi($input);
        $this->pagedata['coupon'] = $coupon['data'];
        
        $this->page('wap/amember/historycoupon.html');
        
    }
    function opinions_image(){
        $order_id = $_REQUEST['order_id'];
        $order = app::get("b2c")->model("orders")->getRow("*",array('order_id'=>$order_id,'member_id'=>$this->member['member_id'],'status'=>'finish'));
        if(!$order){
            
        }
        $goods_id = $_REQUEST['goods_id'];
        $item = app::get("b2c")->model("order_items")->getRow("*",array('order_id'=>$order_id,'goods_id'=>$goods_id,'is_opinions'=>0,'is_comment'=>0));
        if(!$item){
            
        }
        $ck = array("order_id"=>$order_id,"goods_id"=>$goods_id);
        setcookie("opinions_".$this->member['member_id'],  json_encode($ck));
        $this->page('wap/amember/opinions_image.html');
        
    }
    function opinions_tag(){
        $order_id = $_REQUEST['order_id'];
        $order = app::get("b2c")->model("orders")->getRow("*",array('order_id'=>$order_id,'member_id'=>$this->member['member_id'],'status'=>'finish'));
        if(!$order){
            
        }
        $goods_id = $_REQUEST['goods_id'];
        $item = app::get("b2c")->model("order_items")->getRow("*",array('order_id'=>$order_id,'goods_id'=>$goods_id));
        if(!$item){
            
        }
//        if($_REQUEST['image_cut'] == 1){
//
//            $back = imagecreatetruecolor($size['width'], $size['height']);
//            //创建一个可以保存裁剪后图片的资源
//            $cutimg = imagecreatetruecolor($size['width'], $size['height']);
//            //使用imagecopyresampled()函数对图片进行裁剪
//            imagecopyresampled($cutimg,$back,0,0,$_REQUEST['top'],$_REQUEST['left'],$_REQUEST['width'], $_REQUEST['height'],$_REQUEST['width'], $_REQUEST['height']);
//            //保存裁剪 后的图片，如果不想覆盖图片可以为裁剪后的图片加上前缀
//            imagejpeg($cutimg,$_FILES['file']);
//            imagedestroy($cutimg);
//            imagedestroy($back);
//        }
        //TODO 图片切割处理
        $image_id = "0a0131a306f38bd2c99cdb3f4e3a7d8c";//$image_id;
        $this->pagedata['image_id'] = $image_id;
        $ck = json_decode($_COOKIE["opinions_".$this->member['member_id']],true);
        $ck['image'] = $image_id;
        $goods = app::get("b2c")->model("goods")->getRow("*",array("goods_id"=>$ck['goods_id']));
        $this->pagedata['goods_name'] = $goods['name'];
        setcookie("opinions_".$this->member['member_id'],  json_encode($ck));
        
        $this->page('wap/amember/opinions_tag.html');
    } 
    function opinions_add(){
        $ck = json_decode($_COOKIE["opinions_".$this->member['member_id']],true);
        $goods = app::get("b2c")->model("goods")->getRow("*",array("goods_id"=>$ck['goods_id']));
        $tag['x'] = $_POST['x'];
        $tag['y'] = $_POST['y'];
        $tag['image_tag'] = $goods['name'];
        $tag['image_type'] = $_POST['type'];
        $ck['tag'][] = $tag;
        setcookie("opinions_".$this->member['member_id'],json_encode($ck));
        $this->pagedata['tag'] = $ck['tag'];
        
        $this->page('wap/amember/opinions_sub.html');
    } 
    function opinions_sub(){
        $this->begin(array('app'=>'b2c','ctl'=>'wap_amember','act'=>'info'));
        $ck = json_decode($_COOKIE["opinions_".$this->member['member_id']],true);
        $ck['content'] = $_POST['content'];
        $ck['member_id'] = $this->member['member_id'];
        foreach($ck['tag'] as $k=>$t){
            $ck['tag-'.$k."-x"] = $t['x'];
            $ck['tag-'.$k."-y"] = $t['y'];
            $ck['tag-'.$k."-image_type"] = $t['image_type'];
            $ck['tag-'.$k."-image_tag"] = $t['image_tag'];
        }
        unset($ck['tag']);
        $input['method'] = array("file"=>"mobileapi_rpc_goods","action"=>"add_opinions");
        $input['data'] = $ck;
        $opinions = $this->getApi($input);
        if($opinions){
            $this->end(true, app::get('b2c')->_('发布推荐成功'));
        }else{
            $this->end(false, app::get('b2c')->_('发布推荐失败'));
        }
    }
    //图片上传公共类
    protected function imageUpload($filename,$max_size = 3, $size = '') {
        if (empty($size)) {
            $size == array('width' => '400', 'height' => '300');
        }
        if ($filename['size'] > ($max_size * 1024 * 1024)) {
            return array('4003', '上传文件不能超过3M！');
        }

        if ($filename['name'] != "") {
            $type = array("png", "jpg", "gif", "jpeg"); //允许上传的文件

            if (!in_array(strtolower($this->fileext($filename['name'])), $type)) {
                $text = implode(",", $type);
                return array('4003', '您只能上传以下类型文件' . $text);
            }
        }

        $mdl_img = app::get('image')->model('image');

        $image_name = $filename['name'];
        //return $_FILES;
        $image_id = $mdl_img->store($filename['tmp_name'], null, null, $image_name);

        //return $image_id;
        $mdl_img->rebuild($image_id, array('l', 'm', 's'));
        $image_src = base_storager::image_path($image_id, 'l');
        //return 'baid.com';
        if (empty($image_id)) {
            return array('4013', '文件上传失败，请重新上传');
            exit();
        }
        return array(
            'res' => 'success',
            'image_id' => $image_id,
            'image_url' => $image_src,
            'image_name' => $image_name
        );
    }

}
